import 'dart:io';
import 'package:C20Driver/pages/index_page/index_carpool/index_carpool.dart';
import 'package:C20Driver/pages/index_page/index_page.dart';
import 'package:C20Driver/resources/app_storage_interface.dart';
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/services.dart';
import 'package:C20Driver/provider/order_information.dart';
import 'package:C20Driver/provider/record_provider.dart';
// import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:C20Driver/utils/global.dart';
import 'package:provider/provider.dart';
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'pages/general_module/login.dart';
import 'pages/splash_page.dart';
import 'provider/driver_information.dart';
import 'provider/position_information.dart';
import 'routers/routes.dart';
import 'routers/application.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /// 强制竖屏
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }


  await enableFluttifyLog(true); // 关闭log

  // /// 初始化高德地图IOS测试 测试
  // await AmapService.init(
  //     iosKey: '460d5298ce44f5af7e11a14a7808ec31',
  //     androidKey: 'e3dc6e5ad8ee2b398a67bab6c24a379f');
  // await AmapCore.init('460d5298ce44f5af7e11a14a7808ec31');
  // await enableFluttifyLog(true); // 关闭log

  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => PositionInformation()),
      ChangeNotifierProvider(create: (_) => DriverInformation()),
      ChangeNotifierProvider(create: (_) => OrderInformation()),
      ChangeNotifierProvider(create: (_) => RecordProvider()),
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String appToken = '';
  @override
  void initState() {
    super.initState();

    //-------------------路由主要代码start
    final router = FluroRouter();
    Routes.configureRoutes(router);
    Application.router = router;
    //-------------------路由主要代码end
    getLoginState();

  }



  getLoginState() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    appToken = prefs.getString(AppStorageInterface.LOGINTOKEN); // 存储token到本地
    print('token ======= $appToken');
    if (appToken != null && appToken.length > 0) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: MaterialApp(
        title: 'C20司机端',
        //--------------设置组件为中文start
        // localizationsDelegates: [
        //   GlobalMaterialLocalizations.delegate,
        //   GlobalWidgetsLocalizations.delegate,
        //   ChineseCupertinoLocalizations.delegate,
        // ],
        // supportedLocales: Platform.isIOS ? ios : an,
        //--------------设置组件为中文end

        //----------------路由主要代码start
        onGenerateRoute: Application.router.generator,
        //----------------路由主要代码end
        debugShowCheckedModeBanner: false,
        navigatorKey: Global.navigatorKey,
        theme: ThemeData(
          brightness: Brightness.light, // 整体页面背景颜色
          primaryColor: Color.fromARGB(255, 255, 255, 255), // 整体AppBar颜色
          // accentColor: Colors.cyan[600],
        ),
        home: SplashPage()
        // home:  SplashPage(),
        // home: IndexPage(),
      ),
    );
  }
}
